<template>
  <el-main>
      <div style="line-height: 40px;background:	#CAE1FF">
        <h1 style="font-size:25px;color:#4FC1B0;"><i class="el-icon-s-check">修改密码</i></h1>
      </div>
      
     <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
     
      <el-form-item prop="userName">
        <el-input type="hidden" v-model="ruleForm.userName" autocomplete="off"></el-input>
      </el-form-item>
     
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkpassword">
        <el-input type="password" v-model="ruleForm.checkpassword" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="ruleForm.remarks"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button type="success" plain @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>

  </el-main>
</template>

<script>
  export default {
    data() {

      var validatepassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkpassword !== '') {
            this.$refs.ruleForm.validateField('checkpassword');
          }
          callback();
        }
      };
      var validatepassword2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          userName:'',
          password: '',
          checkpassword: '',
          remarks: ''
        },
        rules: {
          password: [
            { validator: validatepassword, trigger: 'blur' }
          ],
          checkpassword: [
            { validator: validatepassword2, trigger: 'blur' }
          ],
        }
      };
    },

    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {

          if (valid) {
            console.log('submit!');
            this.axios({
                method:'post',
                url: "http://localhost:9999/user/user/updatepassword?access_token="+localStorage.getItem("access_token"),
                data: this.ruleForm
            }).then(res=>{
               console.log(res);
               if(res.data.code='200'){
                     this.$notify({
                        title: '成功',
                        message: res.data.msg,
                        type: 'success'
                    });
                    this.$router.replace({ path: "/Home" });
                }else{
                     this.$notify.error({
                        title: '失败',
                        message: res.data.msg
                    });
                }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },

    mounted(){
        var userName = localStorage.getItem("username");
        this.ruleForm.userName=userName;
        console.log(this.ruleForm.userName)
    }
  }
</script>

<style>

</style>